package com.sifa.springcloud.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.sifa.springcloud.entities.CommentResult;
import com.sifa.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
public class CircleBreakerController {

    public static final String serviceName="http://nacos-payment-provider";

    @Resource
    RestTemplate restTemplate;

    @RequestMapping("consumer/fallback/{id}")
    @SentinelResource(value = "fallback",fallback = "handlerFallback"
    ,exceptionsToIgnore = {IllegalArgumentException.class})
    public CommentResult fallBack(@PathVariable("id")Integer id){
        CommentResult result = restTemplate.getForObject(serviceName + "paymentSQL/" + id, CommentResult.class, id);
        if (id==4){
            throw new IllegalArgumentException("IllegalArgumentException,非法参数异常...");
        }else if (result.getData()==null){
            throw new NullPointerException("NullPoint Exception,该Id没有对应记录，空指针异常");
        }
        return result;
    }
    public CommentResult handlerFallback(){
        Payment payment=new Payment();
        return new CommentResult<>(444,"兜底异常");
    }
}
